<?php
namespace app\service;
use think\Db;
define("APPID","64CCC7F9ABB44F598DC260A1B8629089");
define("APPKEY","D53F039BFA2140FB9840DA1095B3BE0E");
define("APPSECRET","3EC20F59D66E40CEA4AF6A66156A9570");
define("TimeStamp","1520971147");
class wotuService{
	private $config=[
                'type'=>'mysql',
                'hostname'=>'127.0.0.1',
                'username'=>'root',
                'password'=>'zaozao2019@',
                'database'=>'entrance',
            ];
	//人员注册
	public function reg($name,$phone,$type,$idNo,$idcardNo){
		$url="http://gs-api.uface.uni-ubi.com/v1/".APPID."/person";
        $token = $this->token();
        $post_data = array(
            "appKey" => APPKEY,
            "token" => $token,
            "name" => $name,	//姓名
            "phone" => $phone,	//手机号码
            "type" => $type,
            "tag"=>"1",
            "idNo"=>$idNo,
            "idcardNo"=>$idcardNo //身份证号
        );
        $data=$this->curl_post($url,$post_data);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        //$arr = json_decode($data, true);
        //$info = $arr['data'];
        return  $data;
	}
    //人员删除
    public function del($guid){
        $token = $this->token();
        $url="http://gs-api.uface.uni-ubi.com/v1/".APPID."/person/".$guid."?token={$token}";
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
        //设置头
        $header = array(
            "Accept: application/json",
            "Content-Type: application/json;charset=utf-8",
        );
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置请求头
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//SSL认证。
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }



    //将图片上传到沃土
    public function uploadimg($base64_img, $personGuid)
    {
        $token = $this->token();
        $url = "http://gs-api.uface.uni-ubi.com/v1/".APPID."/person/{$personGuid}/face";
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        
        $post_data = array(
            "token" => $token,
            "img" => $base64_img
        );
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);

        return $arr;
        

    }
    //将沃土上的图片删除
    public function delimage($personGuid,$guid){
        $appid = APPID;
        $token = $this->token();
        $url = "http://gs-api.uface.uni-ubi.com/v1/{$appid}/person/{$personGuid}/face/{$guid}?token={$token}";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
        $header = array(
            "Accept: application/json",
            "Content-Type: application/json;charset=utf-8",
        );
        //设置头
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置请求头
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//SSL认证。
        $data = curl_exec($ch);
        curl_close($ch);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;
    }


    //授权管理
    public function accredit($guid, $deviceKeys)
    {
        $appid = APPID;
        $url = "http://gs-api.uface.uni-ubi.com/v1/{$appid}/person/{$guid}/devices";
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //设置post数据
        $token = $this->token();
        $post_data = array(
            "token" => $token,
            "deviceKeys" => $deviceKeys,
        );
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        //    print_r($data);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;
        if (isset($arr['data']['content'][0]['guid'])) {
            $guid = $arr['data']['content'][0]['guid'];
        } else {
            $guid = '';
        }

    }

    public function uploadimg2($image, $personGuid)
    {
        $token = $this->token();
       

        $base64_img = $this->base64EncodeImage($image);

        $url = "http://gs-api.uface.uni-ubi.com/v1/".APPID."/person/{$personGuid}/face";
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        
        $post_data = array(
            "token" => $token,
            "img" => $base64_img
        );
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;

        if (isset($arr['data']['faceUrl'])) {
            $photourl = $arr['data']['faceUrl'];
        } else {
            $photourl = '';
        }

        return $photourl;

    }

	public function tupian($guid)
    {

        $appid = APPID;
        $token = $this->token();
        $url = "http://gs-api.uface.uni-ubi.com/v1/{$appid}/person/{$guid}/faces?token={$token}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);#我不需要获取头部啊;
        
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 16);
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        $output = curl_exec($ch);
        curl_close($ch);
        $kais = strpos($output, "{");
        $data = substr($output, $kais);
        $arr = json_decode($data, true);
        return $arr;
        $l = count($arr['data']);
//    echo $l;
        if ($l == 0) {
            $faceUrl = '';
        } else {
            for ($i = 0; $i < $l; $i++) {
                if (!empty($arr['data'][$i]['faceUrl'])) {
                    $faceUrl[] = $arr['data'][$i]['faceUrl'];

                } else {
                    $faceUrl[] = '';
                }
            }
        }
        return $faceUrl;
    }

	public function token()
    {
    	$webinfo= Db::connect($this->config)->table('webset')->find("1");;
    	$time=time();
        if($time-$webinfo["face_token_time"]<36000)
        {
            return $webinfo["face_token"];
        }
        else
        {
        	$url="http://gs-api.uface.uni-ubi.com/v1/".APPID."/auth";
	    	$str=APPKEY.TimeStamp.APPSECRET;
	       	$sign=strtolower(MD5($str));
	        $post_data = array(
	            "appKey" => APPKEY,
	            "timestamp" => TimeStamp,
	            "sign" => $sign
	        );
	       	$data=$this->curl_post($url,$post_data);
	        $kais = strpos($data, "{");
	        $data = substr($data, $kais);
	        $arr = json_decode($data, true);
	        $info["face_token"]=$arr["data"];;
            $info["face_token_time"]=$time;
            $info["id"]="1";
	        Db::connect($this->config)
                  ->table("webset")
                  ->update($info);
	        return $info["face_token"];
        }
    	
        
    }

	public function curl_post($url,$post_data){
		$curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        return $data;//json_decode($data, true);
	}
    public function base64EncodeImage($image_file)
    {
        $base64_image = '';
        $image_info = getimagesize($image_file);
        $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
        $base64_image = chunk_split(base64_encode($image_data));
        return $base64_image;
    }
}